package zhxt.helmet.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import zhxt.helmet.pojo.Camera;
import zhxt.helmet.pojo.User;
import zhxt.helmet.service.CameraService;
import zhxt.helmet.service.CameraThreadManager;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/camera")
public class CameraController {
    @Autowired
    private CameraService cameraService;
    @Autowired
    private CameraThreadManager cameraThreadManager;

    @RequestMapping(value = "/getCameras", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String getCameras(@RequestParam("provinceId") int provinceId,
                             @RequestParam("cityId") int cityId,
                             @RequestParam("districtId")  int districtId,
                             @RequestParam("blockId") int blockId) throws JsonProcessingException {
        List<Camera> cameraList =  cameraService.getCameras(provinceId, cityId, districtId, blockId);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(cameraList);
    }
    //用于数据库管理页面显示全部摄像头
    @RequestMapping("/list")
    @ResponseBody
    public Map<String, Object> cameraList() {
        // 获取用户辖区权限内所有摄像头
        List<Camera> cameraList = cameraService.getCameras(2,2,2,2);
        // 创建对象result，保存查询结果数据
        Map<String, Object> result = new HashMap<String, Object>(2);
        result.put("total", cameraList.size());
        result.put("rows", cameraList);
        return result;
    }

    //用户订阅摄像头
    @PostMapping("/subscribe")
    public ResponseEntity<Map<String, Object>> subscribe(@RequestParam List<Integer> cameras,  HttpSession session) throws FFmpegFrameGrabber.Exception {
         // 从 Session 获取用户对象
        User user = (User) session.getAttribute("user");
        cameraThreadManager.subscribe(cameras, user.getId());
        Map<String, Object> response = new HashMap<>();
        response.put("success", true);
        return ResponseEntity.ok(response); // 返回 JSON 格式

    }

    //用户获取已订阅的摄像头列表, 用于页面加载时显示在选择框
    @PostMapping("/selected")
    @ResponseBody
    public String getSelected( @RequestParam int userId) throws JsonProcessingException {
        List<Integer> cameraIdList = cameraThreadManager.getUserSelectedCamera(userId);
        ObjectMapper objectMapper = new ObjectMapper();
        return objectMapper.writeValueAsString(cameraIdList);
    }
    //新增摄像头
    @PostMapping(value = "/addCamera", produces = "text/html;charset=UTF-8",
            consumes = "application/x-www-form-urlencoded")
    @ResponseBody
    public String addUser( Camera camera) {
        try {
            cameraService.addCamera(camera);
            return "{\"success\":\"true\",\"message\":\"摄像头添加成功\"}";
        } catch (Exception e) {
            return "{\"success\":\"false\",\"message\":\"摄像头添加失败\"}";
        }
    }
    // 修改摄像头
    @RequestMapping(value = "/updateCamera", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String updateCamera( Camera camera) {
        try {
            cameraService.updateCamera(camera);
            return "{\"success\":\"true\",\"message\":\"摄像头修改成功\"}";
        } catch (Exception e) {
            return "{\"success\":\"false\",\"message\":\"摄像头修改失败\",\"errorMsg\":\"失败\"}";
        }
    }
    //删除摄像头
    @RequestMapping(value = "/deleteCamera", produces = "text/html;charset=UTF-8")
    @ResponseBody
    public String deleteCamera(Camera camera) {
        try {
            cameraService.deleteCamera(camera);
            return "{\"success\":\"true\",\"message\":\"摄像头修改成功\"}";
        } catch (Exception e) {
            return "{\"success\":\"false\",\"message\":\"摄像头修改失败\"}";
        }
    }
}
